package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import net.sf.ehcache.pool.Size;
import org.apache.poi.util.SystemOutLogger;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    //  获取容器中ISystemDictionaryItemService的实现类对象，并注入Bean
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //  查询方法
    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionaryItem:list", "字典目录列表"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo) {
        //  调用Service层的query方法
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        //  将获取到的数据设置到模型中
        model.addAttribute("pageResult", pageInfo);

        List<SystemDictionary> systemDictionaries = systemDictionaryService.selectAll();
        model.addAttribute("systemDictionaries", systemDictionaries);

        //  找视图/WEB-INF/views/systemDictionaryItem/list.ftl
        return "systemDictionaryItem/list";
    }

    //  删除方法
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionaryItem:delete", "字典目录删除"}, logical = Logical.OR)
    public JsonResult delete(Long id) {
        try {
            //  判断id是否存在
            if (id != null) {
                //  调用Service层的deleteById方法
                systemDictionaryItemService.deleteById(id);
            }
            return new JsonResult(true, "删除成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult(false, "删除失败");
        }
    }

/*    //  去新增或修改页面方法
    @RequestMapping("/input")
    @RequiredPermission(name = "字典目录编辑", expression = "systemDictionaryItem:input")
    public String input(Long id, Model model){
        //  判断id是否存在
        if (id != null) {
            //  调用Service层的selectById方法
            SystemDictionaryItem systemDictionaryItem = systemDictionaryItemService.selectById(id);
            //  将获取到的数据设置到模型中
            model.addAttribute("systemDictionaryItem", systemDictionaryItem);
        }
        //  找视图/WEB-INF/views/systemDictionaryItem/input.ftl
        return "systemDictionaryItem/input";
    }*/

    //  抽取不填写序号会自动加1的方法
    public SystemDictionaryItem setSystemDictionaryItemSequence(SystemDictionaryItem systemDictionaryItem){
        //  查询当前字典目录的id
        Long parentId = systemDictionaryItem.getParentId();
        //  调用方法查询当前字典目录下面的所有序号
        List<SystemDictionaryItem> items = systemDictionaryItemService.selectByParentId(parentId);
        //  获取集合中最后一个元素值
        SystemDictionaryItem dictionaryItem = items.get(items.size() - 1);
        //  判断当前序号是否为空
        if (systemDictionaryItem.getSequence() == null) {
            //  若为空，则直接调用获取到的最后一个序号值，设置进去并手动加1
            systemDictionaryItem.setSequence(dictionaryItem.getSequence() + 1);
        }
        //  返回systemDictionaryItem对象
        return systemDictionaryItem;
    }

    //  新增或修改页面方法
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionaryItem:saveOrUpdate", "字典目录添加/更新"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        try {
            //  判断id是否存在
            if (systemDictionaryItem.getId() != null) {
                //  若存在，调用Service层的updateById方法
                systemDictionaryItemService.updateById(setSystemDictionaryItemSequence(systemDictionaryItem));
            } else {
                //  若不为空直接插入
                systemDictionaryItemService.insert(setSystemDictionaryItemSequence(systemDictionaryItem));
            }
            return new JsonResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
    }
}
